#coding:utf8
import math
import os,re,json
import time
from collections import defaultdict
import pandas as pd
import numpy as np
import akshare as ak
from utils.logger import Log
from utils.database import DataServer
from utils.get_features import get_latest_5_days, get_feature_data_02
from datetime import datetime


#logger = Log(name=__name__, file_name="").getlog()


def zjlx_change(z1, z2):
    f105 = 0
    if np.sign(z1) > 0:
        if np.sign(z2) <= 0:
            f105 = 1
        elif z1 > z2:
            f105 = 2
        else:
            f105 = 3
    else:
        if np.sign(z2) >= 0:
            f105 = 4
        elif abs(z1) > abs(z2):
            f105 = 5
        else:
            f105 = 6

    return f105


def get_feature_4_code(code, data):
    """

    """


    latest_date = data[0][3]
    last_5_days = get_latest_5_days(latest_date)
    feature = get_feature_data_02(data, last_5_days)
    return feature


def get_data_from_db():
    """

    """

    t1 = datetime.now()
    t2 = t1.strftime('%Y%m%d')
    fileName = "../resources/" + "features_" + t2 + ".csv"

    db = DataServer()
    codes = db.get_all_stock()
    with open(fileName, "w", encoding="utf8") as f:
        for code in codes:
            if re.findall(r"^688|3^|4^|^8|^9", code):
                continue
            data = db.get_all_of_one_code(code)
            features = get_feature_4_code(code, data)
            Y, X = features
            for i in range(len(features)):
                t1 = str(Y[i])
                t2 = ' '.join([str(m) for m in X[i]])
                f.write(t1 + "\t" + t2 + "\n")
            break

if __name__=="__main__":
    filePath = r"../data/history"
    get_data_from_db()
